home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 April / SGI IRIX 6.5 Applications 2004 April.iso / dist / mozilla.idb / var / netscape / mozilla / chrome / messenger.jar.z / messenger.jar / content / messenger-smime / msgCompSMIMEOverlay.js < prev    next >
Text File  |  2003-11-11  |  10KB  |  421 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * 
  13.  * The Initial Developer of the Original Code is Netscape
  14.  * Communications Corporation. Portions created by Netscape are
  15.  * Copyright (C) 1998-2001 Netscape Communications Corporation. All
  16.  * Rights Reserved.
  17.  * 
  18.  * Contributors:
  19.  *   ddrinan@netscape.com
  20.  *   Scott MacGreogr <mscott@netscape.com>
  21.  */
  22.  
  23. const gISMimeCompFields = Components.interfaces.nsIMsgSMIMECompFields;
  24. const gSMimeCompFieldsContractID = "@mozilla.org/messenger-smime/composefields;1";
  25. const gSMimeContractID = "@mozilla.org/messenger-smime/smimejshelper;1";
  26. const gISMimeJSHelper = Components.interfaces.nsISMimeJSHelper;
  27. var gNextSecurityButtonCommand = "";
  28. var gBundle;
  29. var gBrandBundle;
  30. var gSMFields;
  31. var gEncryptedURIService = null;
  32.  
  33.  
  34. function onComposerClose()
  35. {
  36.   gSMFields = null;
  37.   setNoEncryptionUI();
  38.   setNoSignatureUI();
  39.  
  40.   if (!gMsgCompose)
  41.     return;
  42.  
  43.   if (!gMsgCompose.compFields)
  44.     return;
  45.  
  46.   gMsgCompose.compFields.securityInfo = null;
  47. }
  48.  
  49. function onComposerReOpen()
  50. {
  51.   // are we already set up?
  52.   if (gSMFields)
  53.     return;
  54.  
  55.   if (!gMsgCompose)
  56.     return;
  57.  
  58.   if (!gMsgCompose.compFields)
  59.     return;
  60.  
  61.   gMsgCompose.compFields.securityInfo = null;
  62.  
  63.   gSMFields = Components.classes[gSMimeCompFieldsContractID].createInstance(gISMimeCompFields);
  64.   if (gSMFields)
  65.   {
  66.     gMsgCompose.compFields.securityInfo = gSMFields;
  67.     // set up the intial security state....
  68.     var encryptionPolicy = gCurrentIdentity.getIntAttribute("encryptionpolicy");
  69.     // 0 == never, 1 == if possible, 2 == always Encrypt.
  70.     gSMFields.requireEncryptMessage = encryptionPolicy == 2;
  71.  
  72.     gSMFields.signMessage = gCurrentIdentity.getBoolAttribute("sign_mail");
  73.  
  74.     if (gEncryptedURIService && !gSMFields.requireEncryptMessage)
  75.     {
  76.       if (gEncryptedURIService.isEncrypted(gMsgCompose.originalMsgURI))
  77.       {
  78.         // Override encryption setting if original is known as encrypted.
  79.         gSMFields.requireEncryptMessage = true;
  80.       }
  81.     }
  82.  
  83.     if (gSMFields.requireEncryptMessage)
  84.     {
  85.       setEncryptionUI();
  86.     }
  87.     else
  88.     {
  89.       setNoEncryptionUI();
  90.     }
  91.  
  92.     if (gSMFields.signMessage)
  93.     {
  94.       setSignatureUI();
  95.     }
  96.     else
  97.     {
  98.       setNoSignatureUI();
  99.     }
  100.   }
  101. }
  102.  
  103.  
  104. // this function gets called multiple times,
  105. // but only on first open, not on composer recycling
  106. function smimeComposeOnLoad()
  107. {
  108.   if (!gEncryptedURIService)
  109.   {
  110.     gEncryptedURIService = 
  111.       Components.classes["@mozilla.org/messenger-smime/smime-encrypted-uris-service;1"]
  112.       .getService(Components.interfaces.nsIEncryptedSMIMEURIsService);
  113.   }
  114.  
  115.   onComposerReOpen();
  116. }
  117.  
  118. function setupBundles()
  119. {
  120.   if (gBundle && gBrandBundle)
  121.     return;
  122.   
  123.   if (!gBundle) {
  124.     gBundle = document.getElementById("bundle_comp_smime");
  125.     gBrandBundle = document.getElementById("bundle_brand");
  126.   }
  127. }
  128.  
  129. function showNeedSetupInfo()
  130. {
  131.   var ifps = Components.interfaces.nsIPromptService;
  132.  
  133.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  134.   promptService = promptService.QueryInterface(ifps);
  135.   setupBundles();
  136.  
  137.   if (promptService && gBundle && gBrandBundle) {
  138.     var dummy = new Object;
  139.     var buttonPressed =
  140.     promptService.confirmEx(window,
  141.       gBrandBundle.getString("brandShortName"),
  142.       gBundle.getString("NeedSetup"), 
  143.       (ifps.BUTTON_POS_0 * ifps.BUTTON_TITLE_YES
  144.        + ifps.BUTTON_POS_1 * ifps.BUTTON_TITLE_NO),
  145.       0,
  146.       0,
  147.       0,
  148.       null,
  149.       dummy);
  150.     
  151.     if (0 == buttonPressed) {
  152.       openHelp("sign-encrypt");
  153.     }
  154.   }
  155. }
  156.  
  157. function noEncryption()
  158. {
  159.   if (!gSMFields)
  160.     return;
  161.  
  162.   gSMFields.requireEncryptMessage = false;
  163.   setNoEncryptionUI();
  164. }
  165.  
  166. function encryptMessage()
  167. {
  168.   if (!gSMFields)
  169.     return;
  170.   
  171.   var encryptionCertName = gCurrentIdentity.getUnicharAttribute("encryption_cert_name");
  172.   if (!encryptionCertName) 
  173.   {
  174.     gSMFields.requireEncryptMessage = false;
  175.     setNoEncryptionUI();
  176.     showNeedSetupInfo();
  177.     return;
  178.   }
  179.  
  180.   gSMFields.requireEncryptMessage = true;
  181.   setEncryptionUI();
  182. }
  183.  
  184. function signMessage()
  185.   if (!gSMFields)
  186.     return;
  187.  
  188.   // toggle
  189.   gSMFields.signMessage = !gSMFields.signMessage;
  190.  
  191.   if (gSMFields.signMessage) // make sure we have a cert name...
  192.   {
  193.     var signingCertName = gCurrentIdentity.getUnicharAttribute("signing_cert_name");
  194.     if (!signingCertName)
  195.     {
  196.       gSMFields.signMessage = false;
  197.       showNeedSetupInfo();
  198.       return;
  199.     }
  200.  
  201.     setSignatureUI();
  202.   }
  203.   else
  204.   {
  205.     setNoSignatureUI();
  206.   }
  207. }
  208.  
  209. function setSecuritySettings(menu_id)
  210.   if (!gSMFields)
  211.     return;
  212.  
  213.   document.getElementById("menu_securityEncryptRequire" + menu_id).setAttribute("checked", gSMFields.requireEncryptMessage);
  214.   document.getElementById("menu_securityNoEncryption" + menu_id).setAttribute("checked", !gSMFields.requireEncryptMessage);
  215.   document.getElementById("menu_securitySign" + menu_id).setAttribute("checked", gSMFields.signMessage);
  216. }
  217.  
  218. function setNextCommand(what)
  219. {
  220.   gNextSecurityButtonCommand = what;
  221. }
  222.  
  223. function doSecurityButton()
  224. {
  225.   var what = gNextSecurityButtonCommand;
  226.   gNextSecurityButtonCommand = "";
  227.  
  228.   switch (what)
  229.   {
  230.     case "noEncryption":
  231.       noEncryption();
  232.       break;
  233.     
  234.     case "encryptMessage":
  235.       encryptMessage();
  236.       break;
  237.     
  238.     case "signMessage":
  239.       signMessage();
  240.       break;
  241.     
  242.     case "show":
  243.     default:
  244.       showMessageComposeSecurityStatus();
  245.       break;
  246.   }
  247. }
  248.  
  249. function setNoSignatureUI()
  250. {
  251.   top.document.getElementById("securityStatus").removeAttribute("signing");
  252.   top.document.getElementById("signing-status").collapsed = true;
  253. }
  254.  
  255. function setSignatureUI()
  256. {
  257.   top.document.getElementById("securityStatus").setAttribute("signing", "ok");
  258.   top.document.getElementById("signing-status").collapsed = false;
  259. }
  260.  
  261. function setNoEncryptionUI()
  262. {
  263.   top.document.getElementById("securityStatus").removeAttribute("crypto");
  264.   top.document.getElementById("encryption-status").collapsed = true;
  265. }
  266.  
  267. function setEncryptionUI()
  268. {
  269.   top.document.getElementById("securityStatus").setAttribute("crypto", "ok");
  270.   top.document.getElementById("encryption-status").collapsed = false;
  271. }
  272.  
  273. function showMessageComposeSecurityStatus()
  274. {
  275.   Recipients2CompFields(gMsgCompose.compFields);
  276.  
  277.   var encryptionCertName = gCurrentIdentity.getUnicharAttribute("encryption_cert_name");
  278.   var signingCertName = gCurrentIdentity.getUnicharAttribute("signing_cert_name");
  279.   
  280.   window.openDialog('chrome://messenger-smime/content/msgCompSecurityInfo.xul',
  281.     '',
  282.     'chrome,resizable=1,modal=1,dialog=1', 
  283.     {
  284.       compFields : gMsgCompose.compFields,
  285.       subject : GetMsgSubjectElement().value,
  286.       smFields : gSMFields,
  287.       isSigningCertAvailable : (signingCertName.length > 0),
  288.       isEncryptionCertAvailable : (encryptionCertName.length > 0),
  289.       currentIdentity : gCurrentIdentity
  290.     }
  291.   );
  292. }
  293.  
  294. var SecurityController =
  295. {
  296.   supportsCommand: function(command)
  297.   {
  298.     switch ( command )
  299.     {
  300.       case "cmd_viewSecurityStatus":
  301.         return true;
  302.       
  303.       default:
  304.         return false;
  305.      }
  306.   },
  307.  
  308.   isCommandEnabled: function(command)
  309.   {
  310.     switch ( command )
  311.     {
  312.       case "cmd_viewSecurityStatus":
  313.       {
  314.         return true;
  315.       }
  316.  
  317.       default:
  318.         return false;
  319.     }
  320.     return false;
  321.   }
  322. };
  323.  
  324. function onComposerSendMessage()
  325. {
  326.   try {
  327.     if (!gMsgCompose.compFields.securityInfo.requireEncryptMessage) {
  328.       return;
  329.     }
  330.  
  331.     var helper = Components.classes[gSMimeContractID].createInstance(gISMimeJSHelper);
  332.  
  333.     var emailAddresses = new Object();
  334.     var missingCount = new Object();
  335.  
  336.     helper.getNoCertAddresses(
  337.       gMsgCompose.compFields,
  338.       missingCount,
  339.       emailAddresses);
  340.   }
  341.   catch (e)
  342.   {
  343.     return;
  344.   }
  345.  
  346.   if (missingCount.value > 0)
  347.   {
  348.     var prefService =
  349.       Components.classes["@mozilla.org/preferences-service;1"]
  350.         .getService(Components.interfaces.nsIPrefService);
  351.     var prefs = prefService.getBranch(null);
  352.  
  353.     var autocompleteLdap = false;
  354.     autocompleteLdap = prefs.getBoolPref("ldap_2.autoComplete.useDirectory");
  355.  
  356.     if (autocompleteLdap)
  357.     {
  358.       var autocompleteDirectory = null;
  359.       autocompleteDirectory = prefs.getCharPref(
  360.         "ldap_2.autoComplete.directoryServer");
  361.  
  362.       if(gCurrentIdentity.overrideGlobalPref) {
  363.         autocompleteDirectory = gCurrentIdentity.directoryServer;
  364.       }
  365.  
  366.       if (autocompleteDirectory)
  367.       {
  368.         window.openDialog('chrome://messenger-smime/content/certFetchingStatus.xul',
  369.           '',
  370.           'chrome,resizable=1,modal=1,dialog=1', 
  371.           autocompleteDirectory,
  372.           emailAddresses.value
  373.         );
  374.       }
  375.     }
  376.   }
  377. }
  378.  
  379. function onComposerFromChanged()
  380. {
  381.   if (!gSMFields)
  382.     return;
  383.  
  384.   // In order to provide maximum protection to the user:
  385.   // - If encryption is already enabled, we will not turn it off automatically.
  386.   // - If encryption is not enabled, but the new account defaults to encryption, we will turn it on.
  387.   // - If signing is disabled, we will not turn it on automatically.
  388.   // - If signing is enabled, but the new account defaults to not sign, we will turn signing off.
  389.  
  390.   if (!gSMFields.requireEncryptMessage)
  391.   {
  392.     var encryptionPolicy = gCurrentIdentity.getIntAttribute("encryptionpolicy");
  393.     // 0 == never, 1 == if possible, 2 == always Encrypt.
  394.  
  395.     if (encryptionPolicy == 2)
  396.     {
  397.       gSMFields.requireEncryptMessage = true;
  398.       setEncryptionUI();
  399.     }
  400.   }
  401.  
  402.   if (gSMFields.signMessage)
  403.   {
  404.     var signMessage = gCurrentIdentity.getBoolAttribute("sign_mail");
  405.     
  406.     if (!signMessage)
  407.     {
  408.       gSMFields.signMessage = false;
  409.       setNoSignatureUI();
  410.     }
  411.   }
  412. }
  413.  
  414. top.controllers.appendController(SecurityController);
  415. addEventListener('compose-window-close', onComposerClose, true);
  416. addEventListener('compose-window-reopen', onComposerReOpen, true);
  417. addEventListener('compose-send-message', onComposerSendMessage, true);
  418. addEventListener('compose-from-changed', onComposerFromChanged, true);
  419.